// pages/roadmap/bus.js
import { mapTabbar, myMap } from '../../libs/amap-config'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabData:{
      activeType: 'bus',
      mapTabbar
     },
     routeTypes: '最快捷'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      location: options.location
    })
    
  },

  travelStrategy: function () {
    var that = this
    let typeList = ['最快捷', '最经济', '少换乘', '少步行', '不乘地铁']
    wx.showActionSheet({
      itemList: typeList,
      success: function (res) {
        that.setData({
          routeTypes:typeList[res.tapIndex]
        })
        let typeNum = res.tapIndex === 4 ? res.tapIndex + 1 : res.tapIndex
        // 将获取到出行方式的,传递给获取公交路线的函数
        that.getBusRoute(typeNum)
      }
    })
  },

  // 获取到公交线路
  getBusRoute: function (type) {
    var that = this
    // 出发地的经纬
    let origin = wx.getStorageSync('longitude') + ',' + wx.getStorageSync('latitude')
     // 目的地的经纬
    let destination = that.data.location
    // 当前城市
    let city = wx.getStorageSync('city')
    myMap.getTransitRoute({
      origin,
      destination,
      city,
      type,
      success: function (data) {
        console.log(data)
        if (data && data.transits) {
          let transits = data.transits
          for (let i = 0; i < transits.length; i++) {
            let segments = transits[i].segments
            transits[i].transport = [];
            for (let j = 0; j < segments.length; j++){
              if( segments[j].bus && segments[j].bus.buslines && segments[j].bus.buslines[0]){
                let name = segments[j].bus.buslines[0].name
                if (j!==0) {
                  name = ' -> ' + name;
                }
                // 将所有的name添加到一个数组里面
                transits[i].transport.push(name);
              }
            }
            transits[i].transportName = transits[i].transport.join()
            transits[i].distance = parseFloat(transits[i].distance/1000).toFixed(2)
            transits[i].duration = parseInt(transits[i].duration/60)
          }
          that.setData({
            transits:transits
          })
        }
      //  console.log(that.data.transits);
       
      }
    })
  },

  changType (e) {
    wx.navigateTo({
      url: '/pages/roadmap/' + e.currentTarget.dataset.type + '?location=' + this.data.location,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})